# general imports
import pandas as pd
import numpy as np
import json
import plotly.graph_objects as go
import chart_studio.plotly as py
from plotly.graph_objs import *
# here I used the API of plotly to visualize the data
# it's free to use but need sign up first
username = open(".username").read()
API = open(".API").read()
py.sign_in(username, API)
token = open(".mapbox_token").read()
"""Load Data"""
# in case there are commas in the values
china_df = pd.read_csv('Coronavirus_China.csv', thousands=',', dtype={"Province": str})
china_df = china_df.fillna(0)
global_df = pd.read_csv('Coronavirus_global.csv', thousands=',')
global_df = global_df.fillna(0)
"""Visualize Coronavirus Outbreak in China"""
"""
Since there is no existing plotting setting of Chinese Provinces
on Plotly platform,
To map the data, I editted a geojson file to outline the map and
use plotly to make a chloropleth map
"""
# Load the Json file
with open('china-province.geojson') as ref:
provinceJson = json.load(ref)
"""Plot the number of diagnosed people for different provinces in China"""
fig = go.Figure(go.Choroplethmapbox(
geojson=provinceJson,
locations=china_df.Province,
z=china_df.Diagnosed,
colorscale=[[0, "rgb(255, 222, 156)"],
[1./2000, "rgb(250,164,118)"],
[1./200, "rgb(240,116,110)"],
[1./100, "rgb(227,79,111)"],
[1./50, "rgb(220,57,119)"],
[1./10, "rgb(185,37,122)"],
[1, "rgb(124, 29, 111)"]],
colorbar = ColorBar(
title='Number of People'
),
marker_opacity=0.8,
marker_line_width=0.5))
# Change figure setting
fig.update_layout(mapbox_style="carto-positron",
mapbox_accesstoken=token,
mapbox_zoom=2.8,
# adjusted center of China (to make graph centered)
mapbox_center = {"lat": 37.8617, "lon": 104.1954})
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
fig.update_layout(title="China Coronavirus Map (Diagnosed)")
# Show the figure
fig.show()
"""Plot the mortality for different provinces in China"""
fig = go.Figure(go.Choroplethmapbox(
geojson=provinceJson,
locations=china_df.Province,
z=china_df.Death,
colorscale=[[0, "rgb(255, 255, 255)"],
[0.001, "rgb(217,217,217)"],
[0.005, "rgb(189,189,189)"],
[0.01, "rgb(150,150,150)"],
[0.1, "rgb(115,115,115)"],
[0.5, "rgb(82,82,82)"],
[1, "rgb(0,0,0)"]],
colorbar = ColorBar(
title='Number of People'
),
marker_opacity=0.8,
marker_line_width=0.5))
# Change figure setting
fig.update_layout(mapbox_style="carto-positron",
mapbox_accesstoken=token,
mapbox_zoom=2.8,
# adjusted center of China (to make graph centered)
mapbox_center = {"lat": 37.8617, "lon": 104.1954})
fig.update_layout(title="China Coronavirus Map (Death)")
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
# Show the figure
fig.show()
"""Plot the number of healed people for different provinces in China"""
fig = go.Figure(go.Choroplethmapbox(
geojson=provinceJson,
locations=china_df.Province,
z=china_df.Healed,
colorscale=[[0, "rgb(228, 241, 225)"],
[0.001, "rgb(180,217,204)"],
[0.005, "rgb(137,192,182)"],
[0.01, "rgb(99,166,160)"],
[0.1, "rgb(68,140,139)"],
[0.5, "rgb(40,14,116)"],
[1, "rgb(13, 88, 95)"]],
colorbar = ColorBar(
title='Number of People'
),
marker_opacity=0.8,
marker_line_width=0.5))
# Change figure setting
fig.update_layout(mapbox_style="carto-positron",
mapbox_accesstoken=token,
mapbox_zoom=2.8,
# adjusted center of China (to make graph centered)
mapbox_center = {"lat": 37.8617, "lon": 104.1954})
fig.update_layout(title="China Coronavirus Map (Healed)")
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
# Show the figure
fig.show()
"""Visualize Global Coronavirus Outbreak"""
"""
Compared to visualizing data of coronavirus outbreak in China,
visualize global data is much easier with the support of
plotly functions.
"""
# data setting
global_data = Data([
Choropleth(
z = global_df.Diagnosed,
colorbar = ColorBar(
title='Number of People'
),
colorscale=[[0, "rgb(255, 222, 156)"],
[1./5000, "rgb(250,164,118)"],
[1./2000, "rgb(240,116,110)"],
[1./1000, "rgb(227,79,111)"],
[1./500, "rgb(220,57,119)"],
[1./100, "rgb(185,37,122)"],
[1, "rgb(124, 29, 111)"]],
hoverinfo='location+z',
locationmode='country names',
locations = global_df.Country,
marker = Marker(
line=Line(
color='rgb(255,255,255)',
width=2
)
),
text = global_df.Country
)
])
# layout setting
layout = Layout(
geo=dict(
showframe=False,
showcoastlines=False,
projection_type='equirectangular'
),
title='Global Coronavirus Map'
)
# show the plot
fig = Figure(data=global_data, layout=layout)
fig.show()
# This function will generate a plotly file to the plotly profile
# In this case, everyone can visit the map with a link
# For above graphs using data from China region
# This step is unavailable since the graphs generated is too big
plot_url = py.plot(fig)